﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CreateYourWorld.DataModels;
using CreateYourWorld.Infrastructure;

namespace CreateYourWorld.Controllers
{
    public class HomeController : Controller
    {
        private IWorldService worldService = null;

        public HomeController(IWorldService WorldService)
        {
            worldService = WorldService;
        }

        [HttpGet]
        public ActionResult Index(string newx, string newy, string attackbutton, string takebutton)
        {
            SessionModel session = null;

            if (!(Session["Session"] is PlayerBaseModel))
            {
                session = worldService.getSession("test@test_cyw.de", "test");
                session.PlayerData = worldService.getPlayerData(session.ID);
                Session["Session"] = session;
            }
            session = Session["Session"] as SessionModel;

            if (!string.IsNullOrEmpty(newx) && !string.IsNullOrEmpty(newy))
            {
                try
                {
                    int x = Convert.ToInt32(newx);
                    int y = Convert.ToInt32(newy);
                    worldService.updatePlayerPosition(session.ID, x, y);
                }
                catch { }
            }
            
            PlayerModel player = worldService.getPlayer(session.ID);
            player.Data = session.PlayerData;

            ViewBag.Message = "Hallo " + player.Data.Name;
            
            ViewData["Player"] = player;

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
